package eu.webhib.webblog.gui.controller;

import eu.webhib.frmwrk.jsf.base.EditBaseController;
import eu.webhib.frmwrk.jsf.utils.JsfUtil;
import eu.webhib.webblog.application.ApplMail;
import eu.webhib.webblog.gui.application.GuiApplication;
import eu.webhib.webblog.domain.User;
import eu.webhib.webblog.service.factory.ServiceFactory;
import eu.webhib.webblog.service.intf.UserService;
import javax.faces.bean.*;
import javax.faces.event.*;
import org.apache.log4j.Logger;

/**
 * Controller of the User Password Reset page
 */
@ManagedBean(name = "pwres")
@SessionScoped
public class PasswordReset extends EditBaseController {

   private static final Logger log = Logger.getLogger(PasswordReset.class);
   private static UserService userService = ServiceFactory.getUserService();
   // User input data
   private String email;
   private boolean passwordReseted = false;

   //
   // User input  ----------------------------------------------------------
   //
   public String getEmail() {
      return email;
   }

   public void setEmail(String email) {
      this.email = email;
   }

   public boolean isPasswordReseted() {
      return passwordReseted;
   }

   //
   // Password Reset ----------------------------------------------------------------------
   //
   public void passwordResetListener(ComponentSystemEvent event) {
      log.info("passwordResetListener");
      if (GuiApplication.isAuthenticated()) {
         JsfUtil.sendRedirect(GuiApplication.getNotAtLoggedInStatePageURI());
         return;
      }
      if (JsfUtil.isGet()) {
         setInfoMessage("info_passwordreset_new");
      }
   }

   public String passwordResetAction() {
      log.info("passwordResetAction");
      return new EditActionClosure() {

         @Override
         protected void run() {
            passwordReseted = doPasswordResetAction();
         }
      }.start();
   }

   private boolean doPasswordResetAction() {
      User user = findUserWithConfirmedEmail(email);
      if (user == null) {
         setFieldError("email", "error_nouser_email");
         return false;
      }
      try {
         // set password reset pending and confirmation secret
         user.setPasswordReset();
         userService.save(user);
      } catch (Throwable t) {
         log.error("save user", t);
         setFatalMessage("fatal_database_write");
         return false;
      }

      // send confirmation email to confirmed email address
      if (!ApplMail.sendUserMail(
              user.getEmailConfirmed(),
              "info_passwordreset_mail",
              "info_passwordreset_subject",
              GuiApplication.passwordResetConfirmPage,
              user)) {
         // if there was an error at mail transmission
         setFatalMessage("fatal_sendmail");
         return false;
      }

      // all ok
      setInfoMessage("info_passwordreset_ok", email);
      return true;
   }

   private User findUserWithConfirmedEmail(String email) {
      return userService.findByAttributeSingle("emailConfirmed", email);
   }

   // Password Reset was successful and user may continue
   public String passwordResetContinueAction() {
      JsfUtil.sendRedirect(GuiApplication.getHistroyPageURI());
      return null;
   }
}
